﻿using Microsoft.Extensions.Logging;
using NUnit.Framework.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CommonCater
{
    //我们自己的日志类
    public class log<T> where T : class
    {
        static ILogger<T> logs;

        /// <summary>
        /// LogError打印错误的
        /// </summary>
        /// <param name="logger">错误消息</param>
        public static void LogError(string logger)
        {
            //初始化logger
            logs = InitLogger();
            logs.LogError(logger);
            LogLocal("Erroe", logger);
        }

        /// <summary>
        /// LogError打印错误的异常
        /// </summary>
        /// <param name="logger">错误消息</param>
        public static void LogWarn(string logger)
        {
            //初始化logger
            logs = InitLogger();
            logs.LogWarning(logger);
            LogLocal("Warning", logger);
        }

        /// <summary>
        /// LogDebug打印debug
        /// </summary>
        /// <param name="logger">错误消息</param>
        public static void LogDebug(string logger)
        {
            //初始化logger
            logs = InitLogger();
            logs.LogDebug(logger);
            LogLocal("Debug",logger);
            //写入数据库 todo

        }

        /// <summary>
        /// 初始化日志
        /// </summary>
        /// <returns></returns>
        private static ILogger<T> InitLogger()
        {

            //创建日志工厂
            ILoggerFactory factory = LoggerFactory.Create(a => a.AddConsole());
            //创建日志对象
            return factory.CreateLogger<T>();
        }
     
        /// <summary>
        /// 打印到本地文件
        /// </summary>
        /// <param name="level"></param>
        /// <param name="msg"></param>
        private static void LogLocal(string level, string msg)
        {
            //路径  不能写死
            //获取到项目的路径
            string path= Environment.CurrentDirectory;

            //E:\c#体系课\05三层项目-餐饮管理系统\code\餐饮管理系统\CommonCater\bin\Debug

            int o = path.LastIndexOf("餐饮管理系统");
            if (o != -1)
            {
                path = path.Substring(0, o + 6);
            }

            //:\c#体系课\05三层项目-餐饮管理系统
            Directory.CreateDirectory(path+ "\\logger");
            Console.WriteLine("创建文件夹成功");

            string Newpath = $"{path}\\logger\\{DateTime.Now.Year.ToString()}年{DateTime.Now.Month.ToString()}月{DateTime.Now.Day.ToString()}日.txt";

            //判断文件是否存在
            if (!File.Exists(Newpath))
            {
                var sw = File.CreateText(Newpath);//创建文件
                sw.Close();//关闭流
                Console.WriteLine("创建文件成功");
            }

            //往路径写入  对消息进行拼接
            msg = $"{level}     {DateTime.Now.ToString()}    {typeof(T).Name}     {msg} \r\n";
            Console.WriteLine(msg);

                byte[] buffer = Encoding.UTF8.GetBytes(msg);
                using (FileStream file = new FileStream(Newpath, FileMode.Append, FileAccess.Write))
                {
                    file.Write(buffer, 0, buffer.Length);
                }

                Console.WriteLine("写入成功");

        }

    }
}
